<?php

namespace App\Http\Controllers\App;

use App\Models\Queue;
use App\Models\Lines;
use App\Models\Shop;
use App\Models\ShopUserCenter;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

class QueueController extends Controller
{
    /**
     * 队列详情(GET) api/queue/info
     */
    public function info(Request $request)
    {   
        // 测试数据 -- 正式运营时关闭
        // $request->session()->put('user_id',17);
        
        // 验证用户是否登陆
        $user_id = session('user_id');
        if(!$user_id){
            return $this->error('未登录');
        }

        // 商品id
        $shop_id = $request->input('shop_id');
        if(!$shop_id){
            return $this->error('商户参数有误');
        }

        // 用户、商户绑定关系
        $user_shop =  ShopUserCenter::where('user_id',$user_id)->where('shop_id',$shop_id)->where('status',1)->select('balance')->first();
        if(!$user_shop){
            return $this->error('该用户与商户无绑定关系');
        }

        // 获取商品详情
        $info = Queue::where('shop_id',$shop_id)
                ->select('id','name','min_amount','max_amount','poundage','desc','protocol','status','close_reason')
                ->first();
        if(!$info){
            return $this->error('该商户暂无有效队列');
        }

        $info['protocol'] = url('api/queue/protocol?id='.$info['id']);

        // 用户积分（余额）
        $info['balance'] = floatval($user_shop['balance']); 

        // 用户所在本队列的订单
        $info['orders'] = DB::table('wd_bf_lines as l')
                ->leftJoin('wd_system_shop as s' , 'l.delivery_point' , '=' ,'s.id')
                ->leftJoin('wd_bf_queue as q' , 'l.queue' , '=' ,'q.id')
                ->where('queue',$info['id'])
                ->where('l.user_id',$user_id)
                ->where('l.status','!=',0)
                ->select('l.order_id','l.money','l.take_no','l.take_delivery','l.pay_time','l.status','l.note','s.shopname','s.longitude','s.latitude','q.name')
                ->orderBy('l.pay_time','desc')
                ->paginate(10);

        // 商户名称
        $info['name'] = Shop::where('id',$shop_id)->value('shopname');

        // 用户订单总数
        $info['all_count'] = Lines::where('queue',$info['id'])
                            ->where('user_id',$user_id)
                            ->where('status','!=',0)
                            ->count();

        // 返还订单总数
        $info['return_count'] = Lines::where('user_id',$user_id)
                                ->where('queue',$info['id'])
                                ->where('status','>',1)
                                ->count();

        // 获得的辅助助推收益
        $info['assist_proceeds'] = Lines::where('user_id',$user_id)
                                    ->where('queue',$info['id'])
                                    ->sum('assist_proceeds');

        return self::json($info);
    }

    /**
     * 队列协议(GET) api/queue/protocol 
     */
    public function protocol(Request $request)
    {
        $id = $request->input('id');
        $content = Queue::where('id', $id)->value('protocol');
       
        header('Content-type: text/html; charset=utf-8');

        $html  = '<html lang="zh-CN">
                  <meta charset="utf-8">
                  <meta name="viewport" content="width=device-width, initial-scale=1">
                  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
                  <style>img{width: 100% !important; height: auto !important }</style>';

        $html .= $content;
        $html .= '</html>';
        echo $html;
    }

    /**
     * 用户绑定商户信息(GET) api/queue/shopinfo
     */
    public function shopinfo(Request $request)
    {
        $id = $request->input('id');
        if(!$id){
            return $this->error('商户参数缺省');
        }

        $info = Shop::where('id',$id)->select('id','shopname','shopuser','phone','address','longitude','latitude')->first();

        return $this->json($info);
    }

    /**
     * 数据处理
     */
    private function dealData($data)
    {
        return $data;
    }

}